package com.example.duckshop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.duckshop.R;
import com.example.duckshop.bean.Shop;
import com.example.duckshop.util.SPUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;
import java.util.List;
//适配器目的就是将数据与控件进行适配调控达到我们想要的结果，只需要将数据传输到自定义的适配器里就可以达到理想的效果
//SHopAdapter
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {
    private List<Shop> list =new ArrayList<>();
    private Context mActivity;
    private ItemListener mItemListener;
    public void setItemListener(ItemListener itemListener){
        this.mItemListener = itemListener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view= LayoutInflater.from(mActivity).inflate(R.layout.item_rv_shop_list,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    //onBindViewHolder()方法作用是对具体列表进行数据赋值，事件添加等操作
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Shop shop = list.get(i);
        if (shop != null) {
            viewHolder.title.setText(shop.getTitle());
            viewHolder.tvPrice.setText(shop.getPrice());
            Glide.with(mActivity)
                    .asBitmap()
                    .load(shop.getImg())
                    .error(R.drawable.ic_error)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(viewHolder.img);
            Integer userType = (Integer) SPUtils.get(mActivity,SPUtils.USER_TYPE,0);
            Integer userId = (Integer) SPUtils.get(mActivity,SPUtils.USER_ID,0);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemListener!=null){
                        mItemListener.ItemClick(shop);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }//返回列表数目
    public void addItem(List<Shop> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd!=null){
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView tvPrice;
        private ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            tvPrice = itemView.findViewById(R.id.tv_price);
            img = itemView.findViewById(R.id.iv_photo);
        }
    }

    public interface ItemListener{
        void ItemClick(Shop shop);
        void Delete(Integer id);
    }
}
